﻿using GDGeek;
using System;
using System.Collections;
using System.Collections.Generic;
using UnityEngine;

namespace MrPP.SerriedCabinet
{
    public class CellManager : GDGeek.Singleton<CellManager>
    {
        /*
        public enum Type { 

            Hlight,
            Check,
            Shelve,

        }
        */
       // public Action<Type> onState;
      //  public Func<Type> getState;
       // private Type type_ = Type.Hlight;
        public Type type {
            get;
            set;
        
        }


        private FSM fsm_ = new FSM();


        void Start()  
        {
            Model.Instance.search.onOpen += doOpen;
           // getState = delegate { return type; };

            fsm_.addState("hlight", hlight());
            fsm_.addState("check", check());
            fsm_.addState("shelve", shelve());
            fsm_.init("hlight");

            this.doOpen(Model.Instance.search.isOpen, Model.Instance.search.isOpen);

        }
        [SerializeField]
        private CellCheckManager _check;
        [SerializeField]
        private CellShelveManager _shelve;
        [SerializeField]
        private CellHighlightManager _highlight;
        private StateBase shelve()
        {
            State state = new State(); 
            state.onStart += delegate
            {
                _shelve.open();
            };
            state.onOver += delegate
            {
                _shelve.close();
            };
            state.addAction("check", "check");
            state.addAction("shelve", "shelve");
            state.addAction("hlight", "hlight");
            return state;
        }

        private StateBase check()
        {
            State state = new State();
            state.onStart += delegate
            {
                _check.open();
            };
            state.onOver += delegate
            {
                _check.close();
            };
            state.addAction("check", "check");
            state.addAction("shelve", "shelve");
            state.addAction("hlight", "hlight");
            return state;
        }

        private StateBase hlight()
        {
            State state = new State(); 
            state.onStart += delegate
            {
                _highlight.open();
            };
            state.onOver += delegate
            {
                _highlight.close();
            };
            state.addAction("check", "check");
            state.addAction("shelve", "shelve");
            state.addAction("hlight", "hlight");
            return state;
        }

        void OnDestroy() {
           // if (Model.IsInitialized) { 
            //    Model.Instance.search.onOpen -= doOpen;
           // }
        }

        private void doOpen(int oldValue, int newValue)
        {
         //   Cell[] list = this.gameObject.GetComponentsInChildren<Cell>();
         //   foreach (var cell in list) {
          //      cell.reset();
           // }
            switch (newValue) {
                case -1:
                case 0:
                    fsm_.post("hlight");
                   // this.doHlight();
                    break;
                case 1:
                    fsm_.post("check");
                  //  this.doCheck();
                    break;
                case 2:
                    fsm_.post("shelve");
                  //  this.doShelve();
                    break;

            }
        }
        /*
        public void doShelve()
        {
            type = Type.Shelve;
            onState?.Invoke(type);
        }

        public void doCheck()
        {

            type = Type.Check;
            onState?.Invoke(type);
        }

        public void doHlight()
        {

            type = Type.Hlight;
            onState?.Invoke(type);

        }
        */
    }
}